package com.wicket.okrframework.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wicket.okrframework.base.service.MMetadataObjectPropertyService;
import com.wicket.okrframework.base.service.dto.req.BatchQueryObjAttributeListReq;
import com.wicket.okrframework.base.service.dto.req.QueryObjAttributeDetailReq;
import com.wicket.okrframework.base.service.dto.req.QueryObjAttributeListReq;
import com.wicket.okrframework.common.dto.FieldInfo;
import com.wicket.okrframework.common.exception.BizException;
import com.wicket.okrframework.common.util.CommonFunctionHelper;
import com.wicket.okrframework.dal.mapper.mbg.MetadataObjectPropertyMapper;
import com.wicket.okrframework.dal.po.mbg.MetadataCode;
import com.wicket.okrframework.dal.po.mbg.MetadataObjectProperty;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @since 2022/1/19 15:39
 *
 *
 * @version 1.0
 */
@Service
public class MMetadataObjectPropertyServiceImpl
        extends ServiceImpl<MetadataObjectPropertyMapper, MetadataObjectProperty>
        implements MMetadataObjectPropertyService {

    @Autowired
    MetadataObjectPropertyMapper metadataObjectPropertyMapper;

    /**
   * 1-1-04查询对象属性列表[220]
   * gen by moon at 9/2/2022, 2:56:16 PM
   */
 @Trace(operationName = "1-1-04查询对象属性列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<MetadataObjectProperty>  queryObjAttributeList(QueryObjAttributeListReq reqDto) {
     
        try {
            QueryWrapper<MetadataObjectProperty> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataObjectProperty.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataObjectProperty.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataObjectProperty.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
queryWrapper.orderByDesc("operate_time");
            return ((List<MetadataObjectProperty>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-1-04批量查询对象属性列表[990]
   * gen by moon at 9/2/2022, 2:56:16 PM
   */
 @Trace(operationName = "1-1-04批量查询对象属性列表")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  List<MetadataObjectProperty>  batchQueryObjAttributeList(BatchQueryObjAttributeListReq reqDto) {
     
        try {
            QueryWrapper<MetadataObjectProperty> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("TRUE");
            if(reqDto.getObjAttributeIdList()==null || reqDto.getObjAttributeIdList().isEmpty()){
                    return new ArrayList<>();
                }
        queryWrapper.in("object_property_id", reqDto.getObjAttributeIdList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(MetadataObjectProperty.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(MetadataObjectProperty.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper,likeList,fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap,likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(MetadataObjectProperty.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            
            return ((List<MetadataObjectProperty>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1",e,false);
        }
  }

    /**
   * 1-1-04查询对象属性详情[16]
   * gen by moon at 9/2/2022, 2:56:15 PM
   */
 @Trace(operationName = "1-1-04查询对象属性详情")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  MetadataObjectProperty  queryObjAttributeDetail(QueryObjAttributeDetailReq reqDto) {
     
        try {
         reqDto.setIsValid("FALSE");
            QueryWrapper<MetadataObjectProperty> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<MetadataObjectProperty> retList = super.list(queryWrapper);
            if(retList!=null && retList.size()>0){
                MetadataObjectProperty retData = retList.get(0);

                return retData;
            }
            return null;
      } catch (Exception e) {
          e.printStackTrace();
          throw new BizException("-1",e,false);
      }
  }

    /**
   * 1-1-04删除对象属性[15]
   * gen by moon at 9/2/2022, 2:56:15 PM
   */
 @Trace(operationName = "1-1-04删除对象属性")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  deleteObjAttribute(String objectPropertyId ) {
     
      return super.removeById(objectPropertyId);
  }

    /**
   * 1-1-04修改对象属性[14]
   * gen by moon at 9/2/2022, 2:56:14 PM
   */
 @Trace(operationName = "1-1-04修改对象属性")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  boolean  updateObjAttribute(MetadataObjectProperty metadataObjectProperty) {
     metadataObjectProperty.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
metadataObjectProperty.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(metadataObjectProperty!=null && metadataObjectProperty.getSubjectLifeCycle()!=null &&  metadataObjectProperty.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataObjectProperty.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
if(metadataObjectProperty!=null && metadataObjectProperty.getSubjectLifeCycle()!=null && metadataObjectProperty.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             metadataObjectProperty.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
        return super.updateById(metadataObjectProperty);
  }

    /**
   * 1-1-04新增对象属性[13]
   * gen by moon at 9/2/2022, 2:56:13 PM
   */
 @Trace(operationName = "1-1-04新增对象属性")
 @Tags({@Tag(key = "参数",value = "arg[0]"),
 @Tag(key = "返回值",value = "returnedObj")})
  @Override
  @Transactional(rollbackFor=Exception.class)
  public  String  addObjAttribute(MetadataObjectProperty metadataObjectProperty) {
     metadataObjectProperty.setObjectPropertyId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
metadataObjectProperty.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
metadataObjectProperty.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
metadataObjectProperty.setIsArchive("FALSE");
metadataObjectProperty.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
if(metadataObjectProperty!=null && metadataObjectProperty.getSubjectLifeCycle()!=null &&  metadataObjectProperty.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            metadataObjectProperty.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
          }
metadataObjectProperty.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
if(metadataObjectProperty!=null && metadataObjectProperty.getSubjectLifeCycle()!=null && metadataObjectProperty.getSubjectLifeCycle().equals("CURRENTLY_VALID")){
             metadataObjectProperty.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
            }
metadataObjectProperty.setIsValid("FALSE");
metadataObjectProperty.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
metadataObjectProperty.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
     if(super.save(metadataObjectProperty)){
        return metadataObjectProperty.getObjectPropertyId();
     }else {
        return "-1";
     }
    
  }
}
